package com.alison;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @Author alison
 * @Date 2024/4/17 14:56
 * @Version 1.0
 * @Description
 */
public class ReadTextToRDD {

    public static void main(String[] args) {
        // configure spark
        SparkConf sparkConf = new SparkConf().setAppName("Read Text to RDD")
                .setMaster("local[2]").set("spark.executor.memory","1g");
        // start a spark context
        JavaSparkContext javaSparkContext = new JavaSparkContext(sparkConf);
        // build List<String>
        List<String> list = Arrays.asList("a:1", "a:2", "b:1", "b:1", "c:1","d:1");
        // List<String> to JavaRDD<String>
        JavaRDD<String> javaRDD = javaSparkContext.parallelize(list);
        JavaRDD<Row> rowJavaRDD = javaRDD.map(s -> {
            String[] ss = s.split(":");
            return RowFactory.create(ss[0], ss[1]);
        });
        rowJavaRDD.collect().forEach(System.out::println);
    }
}
